package org.sample;

import org.sample.commons.io.FileUtils;
import org.sample.commons.io.IOUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class Spider {

    private static final String SOURCE = "E:\\source\\";

    private static final Map<String, String> Range = new HashMap<>(400);
    private static final Map<String, Integer> Stats = new HashMap<>(400);

    private static final Stack<String> Backlog = new Stack<>();
    private static final Set<String> Complete = new HashSet<>(400);

    public static void main(String[] args) throws Exception {

        File[] files = new File(SOURCE).listFiles();
        for (File file : files) {
            String name = file.getName();
            Range.put(name, FileUtils.read(file));
            Stats.put(name, 0);
        }
        Range.remove("BasicData.java");
        Range.remove("BasicDataDao.java");
        Range.remove("Constants.java");
        Range.remove("DateUtil.java");
        Range.remove("IBasicDataDao.java");
        Range.remove("IEntity.java");
        Range.remove("JT808Constants.java");
        Range.remove("StringUtil.java");
        Range.remove("T809Constants.java");
        Range.remove("QueryResult.java");
        Range.remove("JsonMessage.java");
        Range.remove("QueryService.java");
        Range.remove("SystemConfig.java");
        Range.remove("TreeNode.java");
//        Range.remove("xx");
//        Range.remove("xx");

//        Backlog.push("DepartmentAction.java");
//        Backlog.push("VehicleAction.java");
//        Backlog.push("TerminalInfoAction.java");
//        Backlog.push("DriverInfoAction.java");
//        Backlog.push("MemberInfoAction.java");

//        Backlog.push("FunctionModelAction.java");
//        Backlog.push("UserAction.java");
//        Backlog.push("RoleAction.java");
//        Backlog.push("DepartmentAction.java");

//        Backlog.push("ReportAction.java");
//        Backlog.push("DepartmentAction.java");
//        Backlog.push("BasicDataAction.java");

//        Backlog.push("BasicDataAction.java");
//        Backlog.push("ReportAction.java");
//        Backlog.push("DepartmentAction.java");


//        Backlog.push("TerminalCommandAction.java");
//        Backlog.push("TerminalParamAction.java");
//        Backlog.push("VehicleAction.java");
//        Backlog.push("PlatformCommandAction.java");


        Backlog.push("RealDataAction.java");
        Backlog.push("PlatformCommandAction.java");
        Backlog.push("ReportAction.java");
        Backlog.push("DepartmentAction.java");


        while (!Backlog.isEmpty()) {
            crawl(Backlog.pop());
        }


        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\Users\\admin\\Documents\\新建文件夹\\Map.txt"), StandardCharsets.UTF_8));
        for (String s : Complete) {
//                writer.write(s);
//                writer.write("\r\n");

            Stats.put(s, Stats.get(s) + 1);
        }

        for (String s : Complete) {
            writer.write(Range.get(s));
        }
        IOUtils.close(writer);

        TreeSet<String> t = new TreeSet<>();

        for (Map.Entry<String, Integer> entry : Stats.entrySet())
            if (entry.getValue() > 20)
                t.add(entry.getValue() + "\t" + entry.getKey());

        for (String s : t)
            System.out.println(s);
    }

    private static void crawl(String root) throws Exception {
        if ("TerminalCommandService".equalsIgnoreCase(root))
            System.out.println(root);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(SOURCE + root), StandardCharsets.UTF_8));
        String line;
        while ((line = reader.readLine()) != null) {

            if (line.startsWith("public"))
                break;

            if (line.startsWith("import")) {
                line = line.trim();
                int i = line.lastIndexOf(".");
                line = line.substring(i + 1, line.length() - 1).trim();
                String filename = line + ".java";

                if (!Complete.contains(filename) && Range.containsKey(filename)) {
                    Backlog.add(filename);
                    filename = filename.substring(1, filename.length());

                    if (!Complete.contains(filename) && Range.containsKey(filename))
                        Backlog.add(filename);

                    filename = line + "Impl.java";
                    if (!Complete.contains(filename) && Range.containsKey(filename))
                        Backlog.add(filename);
                }
            }
        }

        IOUtils.close(reader);
        Complete.add(root);
    }
}